package com.world.business.belief.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.world.business.belief.domain.Belief;
import com.world.business.belief.domain.query.BeliefAddRequest;
import com.world.business.belief.domain.query.BeliefPageRequest;
import com.world.business.belief.domain.query.BeliefUpdateRequest;
import com.world.business.belief.domain.vo.BeliefVO;
import com.world.business.belief.mapper.BeliefMapper;
import com.world.business.belief.service.IBeliefService;
import com.world.common.enumeration.DelFlagEnum;
import com.world.common.exception.BusinessException;
import com.world.common.security.SecurityUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 信仰 服务实现类
 * </p>
 *
 * @author 邓驴
 * @since 2023-07-31
 */
@Service
public class BeliefServiceImpl extends ServiceImpl<BeliefMapper, Belief> implements IBeliefService {

    @Override
    public IPage<BeliefVO> getPage(IPage<Belief> page, BeliefPageRequest request) {
        return this.baseMapper.getPage(page, request);
    }

    @Override
    public Long add(BeliefAddRequest request) {
        Belief belief = BeanUtil.copyProperties(request, Belief.class);
        belief.setCreateBy(SecurityUtils.getLoginUser().getName());
        belief.setCreateById(SecurityUtils.getLoginUser().getId());
        belief.setCreateTime(LocalDateTime.now());
        this.baseMapper.insert(belief);
        return belief.getId();
    }

    @Override
    public void update(BeliefUpdateRequest request) {
        LambdaQueryWrapper<Belief> queryWrapper = Wrappers.lambdaQuery(Belief.class);
        queryWrapper.eq(Belief::getDelFlag, DelFlagEnum.NORMAL.getCode());
        queryWrapper.eq(Belief::getId, request.getId());
        Belief one = this.baseMapper.selectOne(queryWrapper);
        if (BeanUtil.isEmpty(one)) {
            throw new BusinessException(HttpStatus.HTTP_BAD_REQUEST, "数据不存在");
        }

        LambdaUpdateWrapper<Belief> updateWrapper = Wrappers.lambdaUpdate(Belief.class);
        updateWrapper.eq(Belief::getId, request.getId());
        updateWrapper.set(StrUtil.isNotBlank(request.getBeliefName()), Belief::getBeliefName, request.getBeliefName());
        updateWrapper.set(request.getAreaId() != null, Belief::getAreaId, request.getAreaId());
        updateWrapper.set(StrUtil.isNotBlank(request.getStatus()), Belief::getStatus, request.getStatus());
        updateWrapper.set(StrUtil.isNotBlank(request.getDescription()), Belief::getDescription, request.getDescription());
        updateWrapper.set(StrUtil.isNotBlank(request.getRemark()), Belief::getRemark, request.getRemark());
        updateWrapper.set(Belief::getUpdateBy, SecurityUtils.getLoginUser().getName());
        updateWrapper.set(Belief::getUpdateById, SecurityUtils.getLoginUser().getId());
        updateWrapper.set(Belief::getUpdateTime, LocalDateTime.now());
        this.baseMapper.update(null, updateWrapper);
    }

    @Override
    public void delete(Long id) {
        LambdaUpdateWrapper<Belief> updateWrapper = Wrappers.lambdaUpdate(Belief.class);
        updateWrapper.eq(Belief::getId, id);
        updateWrapper.set(Belief::getDelFlag, DelFlagEnum.DELETE.getCode());
        updateWrapper.set(Belief::getUpdateBy, SecurityUtils.getLoginUser().getName());
        updateWrapper.set(Belief::getUpdateById, SecurityUtils.getLoginUser().getId());
        updateWrapper.set(Belief::getUpdateTime, LocalDateTime.now());
        this.baseMapper.update(null, updateWrapper);
    }
}
